package com.cloudserver.springboot.utils;

import com.cloudserver.springboot.Constants.MapInfo;

import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * @author zqq
 * @create 2020-10-08 12:03
 */
public class MapUntil extends LinkedHashMap {

    private final int maxSize;

    private int size;

    public MapUntil(){
        this.maxSize = MapInfo.MAX_SIZE;
    }

    @Override
    public Object put(Object key, Object value) {
        if (size == maxSize){
            int i = 0;
            Iterator iterator = this.entrySet().iterator();
            while (iterator.hasNext()){
                iterator.next();
                iterator.remove();
                i++;
                if (i > size/2){
                    break;
                }
            }
        }
        if (this.containsKey(key)){
            return null;
        }
        this.size++;
        return super.put(key, value);
    }
}